package com.kedacom.demo.rabbitmq_java_client.ch5.topics;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class EmitLogTopic {

	public static void main(String[] args) throws IOException, TimeoutException {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(Constants.RABBITMQ_HOST);
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.exchangeDeclare(Constants.EXCHANGE_NAME, "topic");

		String routingKey = "moooo.info";
		String message = "this is a(an) " + routingKey + " message.";
		sendMessage(channel, Constants.EXCHANGE_NAME, routingKey, message);
		routingKey = "truesocial.info";
		message = "this is a(an) " + routingKey + " message.";
		sendMessage(channel, Constants.EXCHANGE_NAME, routingKey, message);
		routingKey = "truesocial.debug";
		message = "this is a(an) " + routingKey + " message.";
		sendMessage(channel, Constants.EXCHANGE_NAME, routingKey, message);

		channel.close();
		connection.close();
	}

	private static final void sendMessage(Channel channel, String exchangeName, String routingKey, String message)
			throws IOException {
		channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
		System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
	}

}
